package com.itheima.goods.controller;

import com.itheima.goods.pojo.Goods;
import com.itheima.goods.service.GoodsService;
import com.itheima.goods.vo.ResultInfo;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping
    public ResultInfo findAll(){
       return goodsService.findAll();

    }
    @PostMapping
    public ResultInfo add(@RequestBody Goods goods){
        return goodsService.add(goods);
    }

    @GetMapping(value = "/search/{page}/{size}")
    public ResultInfo findPage(@PathVariable("page")Integer page, @PathVariable("size")Integer size){
        return goodsService.findPage(page,size);
    }

    @GetMapping(value = "/{id}")
    public ResultInfo findById(@PathVariable("id")Integer id){
        return goodsService.findById(id);
    }


    @PutMapping(value = "/{id}")
    public ResultInfo updateGoods(@PathVariable("id")Integer id,@RequestBody Goods goods){
        return goodsService.updateGoods(id,goods);
    }

    @DeleteMapping(value = "/{id}")
    public ResultInfo deleteGoods(@PathVariable("id")Integer id){

        boolean flag = goodsService.removeById(id);
        if (flag) {
            return new ResultInfo(StatusCode.OK,null,true,"删除成功");
        }
        return new ResultInfo().builder().code(StatusCode.ERROR).message("删除失败").flag(false).build();
    }

}
